package com.example.kotlin.jetpack_compose_by_tutorials

import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.myapplication.R

/**
 *Create by Fanketly on 2022/9/17
 *Gitee:https://gitee.com/fanketly
 *desc:
 */
@Composable
fun BookImageGrid() {
//    LazyHorizontalGrid(rows = , content = )
//    LazyVerticalGrid(columns = , content = )
}

@Composable
fun BookImageColumn(modifier: Modifier = Modifier) {
//rememberScrollState    会根据滚动配置创建滚动状态，并在重组期间处理滚动行为，以便不会丢失位置
    //flingBehavior 用于执行具有给定速度的投掷动画。
    Column(modifier = modifier.verticalScroll(rememberScrollState(), flingBehavior = null)) {
        BookImage(imageResId = R.drawable.pic, contentDescriptionResId = R.string.app_name)
        BookImage(imageResId = R.drawable.pic, contentDescriptionResId = R.string.app_name)
        BookImage(imageResId = R.drawable.pic, contentDescriptionResId = R.string.app_name)
        BookImage(imageResId = R.drawable.pic, contentDescriptionResId = R.string.app_name)
    }
}


@Composable
fun BookImageLazyColumn(modifier: Modifier = Modifier) {
    /*   rememberScrollState    会根据滚动配置创建滚动状态，并在重组期间处理滚动行为，以便不会丢失位置
       flingBehavior 用于执行具有给定速度的投掷动画。
       JetpackCompose不需要回收的ViewHolder池，因为它的重组处理缓存更有效*/
    LazyColumn(modifier = modifier) {
        items(6) {
            ListItem(imageResId = R.drawable.pic, contentDescriptionResId = R.string.app_name)
        }
    }
}

@Composable
fun ListItem(
    @DrawableRes imageResId: Int,
    @StringRes contentDescriptionResId: Int
) {
    Column(Modifier.padding(8.dp)) {
        Text(
            text = stringResource(contentDescriptionResId), fontSize = 22.sp,
            fontWeight = FontWeight.Bold,
            color = colorResource(id = R.color.black)
        )
        Spacer(modifier = Modifier.height(8.dp))
        LazyRow {
            items(4) {
                BookImage(
                    imageResId = imageResId,
                    contentDescriptionResId = contentDescriptionResId
                )
            }
        }
    }
}

@Composable
fun BookImage(
    @DrawableRes imageResId: Int,
    @StringRes contentDescriptionResId: Int
) {
    Image(
        bitmap = ImageBitmap.imageResource(imageResId),
        contentDescription = stringResource(contentDescriptionResId),
        contentScale = ContentScale.Fit,
        modifier = Modifier.size(100.dp, 100.dp)
    )
}
